package com.ruoyi.students.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.students.domain.Students;
import com.ruoyi.students.service.IStudentsService;
import com.ruoyi.teacher.domain.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学生基本信息管理Controller
 * 
 * @author yxy
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/students/students")
public class StudentsController extends BaseController
{
    @Autowired
    private IStudentsService studentsService;

    /**
     * 查询学生基本信息管理列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Students students)
    {
        startPage();
        List<Students> list = studentsService.selectStudentsList(students);
        return getDataTable(list);
    }

    /**
     * 导出学生基本信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('students:students:export')")
    @Log(title = "学生基本信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Students students)
    {
        List<Students> list = studentsService.selectStudentsList(students);
        ExcelUtil<Students> util = new ExcelUtil<Students>(Students.class);
        util.exportExcel(response, list, "学生基本信息管理数据");
    }

    /**
     * 获取学生基本信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('students:students:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentsService.selectStudentsById(id));
    }

    /**
     * 新增学生基本信息管理
     */
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody Students students)
    {
        return toAjax(studentsService.insertStudents(students));
    }

    /**
     * 修改学生基本信息管理
     */
    @PreAuthorize("@ss.hasPermi('students:students:edit')")
    @Log(title = "学生基本信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Students students)
    {
        return toAjax(studentsService.updateStudents(students));
    }

    /**
     * 删除学生基本信息管理
     */
    @PreAuthorize("@ss.hasPermi('students:students:remove')")
    @Log(title = "学生基本信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentsService.deleteStudentsByIds(ids));
    }

    /**
     * 验证学生登录信息是否正确 登录功能
     */
    @Anonymous
    @PostMapping(value = "/{studentId}/{password}")
    public AjaxResult studentloginInfo(@PathVariable("studentId") String studentId , @PathVariable("password") String password)
    {
        if(studentId==null||password==null){
            return warn("参数错误");
        }
        return success(studentsService.studentloginInfo(studentId,password));
    }

    /**
     * 根据studentid查询学生信息
     * */
    @Anonymous
    @GetMapping(value = "/getinfo/{sid}")
    public Students selectStudentsByStudentId(@PathVariable("sid") String studentId){
        return studentsService.selectStudentsByStudentId(studentId);
    }

    /**
     * 根据id修改信息
     * */
    @Anonymous
    @PutMapping(value = "/submitinfo")
    public AjaxResult submitinfo(@RequestBody Students students)
    {
        return toAjax(studentsService.updateStudentsByStudentid(students));
    }

    /**
     * 根据学号查询姓名
     */
    @Anonymous
    @GetMapping(value = "/getStudentName/{studentId}")
    public AjaxResult selectStudentName(@PathVariable("studentId") String studentId)
    {
        return  AjaxResult.success(studentsService.selectStudentName(studentId));
    }

    /**
     * 导入学生信息信息 , 批量导入
     */

    @PreAuthorize("@ss.hasPermi('students:students:add')")
    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Students> util = new ExcelUtil<Students>(Students.class);
        List<Students> StudentsList = util.importExcel(file.getInputStream());
        return toAjax(studentsService.batchInsertscoreRequests(StudentsList));
    }

}
